# -*- coding: utf-8 -*-
# @Time : 2018/12/25 9:34
# @Author : Administrator
# @File : WoniuSalesV1.py
# @Project : python_demo
# @title :
import http.client,random,unittest,HTMLTestRunner,requests
class Httplib_Test(unittest.TestCase):
#
    def setUp(self):
        self.cookie=''#初始化cookie
        self.url='192.168.2.172'#初始化主机
        self.port=8080#初始化端口
        self.url_login='/WoniuSales/user/login'#初始化登录的URI资源
        self.header={'Content-Type':'application/x-www-form-urlencoded'}#构建头部
        self.con=http.client.HTTPConnection(self.url,self.port)#建立连接
        self.logindata='username=admin&password=admin123&verifycode=0000'#构建登录的正文数据
        self.con.request('POST',self.url_login,self.logindata,self.header)#发送登录的请求
        self.cookie=self.con.getresponse().getheader('Set-Cookie').split(';')[0]
        # print(self.cookie)
        self.custadd_url='/WoniuSales/customer/add'
        self.con.close()

    def testcustomer_add01(self):#合法录入手机号未注册，注册会员
        num = random.randint(1000, 9999)#随机数
        data_01=('customername=未和&customerphone=1321212%d'\
                '&childsex=女&childdate=2018-11-12&creditkids=0&creditcloth=0'%num).encode()#构建新增会员信息的正文数据
        header={'Content-Type':'application/x-www-form-urlencoded','Cookie':self.cookie}#构建头信息
        self.con = http.client.HTTPConnection(self.url, self.port)#建立连接
        self.con.request('POST',self.custadd_url,data_01,header)#发送新增会员的请求
        res01=self.con.getresponse().read().decode()#获取新增会员接口的响应结果
        print(res01)
        # assert res01=='add-successful'#响应断言
        self.assertEqual(res01,'add-successful')#响应断言
        self.con.close()
#
    def testcustomer_add02(self):#已注册手机号重复注册会员
#
        data_01=('customername=lency&customerphone=18530785881'\
                '&childsex=女&childdate=2018-11-12&creditkids=0&creditcloth=0').encode()
        header={'Content-Type':'application/x-www-form-urlencoded','Cookie':self.cookie}
        self.con = http.client.HTTPConnection(self.url, self.port)
        self.con.request('POST',self.custadd_url,data_01,header)
        res02=self.con.getresponse().read().decode()
        print(res02)
        assert res02=='already-added'
        self.con.close()
if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTests(unittest.TestLoader().loadTestsFromTestCase(Httplib_Test))
    with open('test_report.html', 'w', encoding='utf-8') as file:
        HTMLTestRunner.HTMLTestRunner(stream=file, verbosity=2,
                                      title='Httplib库-接口测试',
                                      description='会员管理模块接口测试').run(suite)
